﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ServiceModel;
using WCFServiceWebRole1.Services;
using System.ServiceModel.Description;
using System.ServiceModel.Web;

namespace WCFServiceWebRole1_Tests
{
    [TestClass]
    public class UnitTest5
    {
        //string baseaddr = "http://localhost:8000/Services/users.svc";
        //Uri baseAddress = new Uri(baseaddr);
        static readonly string ServiceBaseAddress = "http://" + Environment.MachineName + ":8000/Services/users.svc"; 
    static ServiceHost host; 
   // static Dictionary<int, Product> products; 
  
    Iusers proxy; 
  
    [ClassInitialize] 
    public static void ClassInitialize(TestContext context) 
    { 
        host = new ServiceHost(typeof(user)); 
       // host.AddServiceEndpoint(typeof(Iusers), new WebHttpBinding(), ""); 
        host.Open(); 
    } 
  
    [ClassCleanup] 
    public static void ClassCleanup() 
    { 
        host.Close(); 
    } 
  
    [TestInitialize] 
    public void Initialize() 
    {

        WebChannelFactory<Iusers> factory = new WebChannelFactory<Iusers>(new Uri(ServiceBaseAddress));
        factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
        proxy = factory.CreateChannel();

        //ChannelFactory<Iusers> factory = new ChannelFactory<Iusers>(new WebHttpBinding(), new EndpointAddress(ServiceBaseAddress));
        //factory.Endpoint.Behaviors.Add(new WebHttpBehavior());

        //this.proxy = factory.CreateChannel(); 
        //((IClientChannel)proxy).Closed += delegate { factory.Close(); }; 
    } 
  
    [TestCleanup] 
    public void Cleanup() 
    { 
        ((IClientChannel)this.proxy).Close(); 
    }

    [TestMethod]
    public void CheckingUserName()
    {
           Assert.AreEqual(proxy.getUserByUserId("1092").response.Username.ToString(), "ayyappan");
    } 
    }
}
